define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'inventory/kc/index' + location.search,
                    add_url: 'inventory/kc/add',
                    edit_url: 'inventory/kc/edit',
                    del_url: 'inventory/kc/del',
                    multi_url: 'inventory/kc/multi',
                    import_url: 'inventory/kc/import',
                    table: 'kc',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                       // {field: 'id', title: __('Id')},
                       // {field: 'AutoID', title: __('Autoid')},
                        {field: 'whname', title: '仓库名称'},
                        {field: 'shname', title: '货架名称'},
                        {field: 'cWhCode', title: __('Cwhcode'), operate: 'LIKE'},
                        {field: 'cInvCode', title: '物料编码'},
                        {field: 'name', title:"物料名称",operate: false},
                        {field: 'specification', title:"规格型号",operate: 'LIKE',operate: false},
                        // {field: 'cFree1', title:"物料属性",operate: 'LIKE'},
                        {field: 'unit', title:"单位",operate: 'LIKE',operate: false},
                        //{field: 'cBatch', title: __('Cbatch'), operate: 'LIKE',operate: false},
                        {field: 'iQuantity', title: __('Iquantity'), operate:'BETWEEN'},
                       // {field: 'dMdate', title: __('Dmdate'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                       // {field: 'dVDate', title: __('Dvdate'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                       // {field: 'iMassDate', title: __('Imassdate')},
                       // {field: 'dLastCheckDate', title: __('Dlastcheckdate'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                       // {field: 'cCheckState', title: __('Ccheckstate'), operate: 'LIKE'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                       // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            $('.btn-danger').on('click',function(){
                Toastr.info("数据同步中请稍后....");
                $.post('inventory/kc/getData',function(e){
                       if(e.code==1){
                         Toastr.success(e.msg);
                         table.bootstrapTable('refresh');
                       }else{
                         Toastr.error(e.msg);
                       }
                    })
                })
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'inventory/kc/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'inventory/kc/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'inventory/kc/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            $('#choose-shelves').click(function(){
                let house_id = $("#c-warehouse_id").val();
                top.Fast.api.open('information/shelves/choose?warehouse_id='+house_id,'选择货架或库区',{offset: 'r',callback:function(value){
                        $("#c-shelves_id,#c-shelves_name").val(value.id);
                        $("#c-shelves_name").selectPageRefresh();
                    }});
            })
            Controller.api.bindevent();
        },
        choose:function(){
            $('#choose_id').click(function(){
                let id =  Controller.api.selectedids(chooseTable);
                Fast.api.close(id);
            })
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var chooseTable = $('#chooseTable')

            chooseTable.bootstrapTable({
                url: "inventory/kc/choose" + location.search,
                pk: 'id',
                search:false,
                singleSelect:false,
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: 'id',visible:false},
                        {field: 'cInvCode', title: '物料编码', operate: 'LIKE'},
                        {field: 'name', title:"物料名称",operate: 'LIKE'},
                        {field: 'specification', title:"规格型号",operate: 'LIKE'},
                        {field: 'cFree1', title:"物料属性",operate: 'LIKE'},
                        {field: 'unit', title:"单位",operate: 'LIKE'},
                        {field: 'cBatch', title: __('Cbatch'), operate: 'LIKE'},
                        {field: 'iQuantity', title: __('Iquantity'), operate:'BETWEEN'},
                        {field: 'shname', title: '货架'},
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(chooseTable);

        },
        api: {
            selectedids: function (table, current) {
                var options = table.bootstrapTable('getOptions');
                //如果有设置翻页记忆模式
                if (!current && options.maintainSelected) {
                    return options.selectedIds;
                }
                return $.map(table.bootstrapTable('getSelections'), function (row) {
                    return row;
                });
            },
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
